package qsoft.omanga.common.utils;

import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;

/**
 * @author Sontt
 */
public class XMLUtil {
    /**
     * 
     */
    private static final XMLUtil XML_UTIL = new XMLUtil();

    /**
     * 
     * @return XMLUtil
     */
    public static XMLUtil getInstance() {
        return XML_UTIL;
    }

    /**
     * 
     * @param fileName
     *            String
     * @return Document
     */
    public Document loadXMLDoc(String fileName) {
        Document document = null;
        try {
            DocumentBuilderFactory factory = DocumentBuilderFactory
                    .newInstance();
            DocumentBuilder builder = factory.newDocumentBuilder();
            document = builder.parse(new File(fileName));
        } catch (ParserConfigurationException parserError) {
            // handle exception creating DocumentBuilder
            parserError.printStackTrace();
        } catch (IOException fileException) {
            // handle exception reading data from file
            fileException.printStackTrace();
        } catch (SAXException parseException) {
            // handle exception parsing XML document
            parseException.printStackTrace();
        }
        return document;
    }

    /**
     * 
     * @param doc
     *            Document
     * @param expression
     *            String
     * @return NodeList getNodeList
     */
    public NodeList getNodeList(Document doc, String expression) {
        try {
            // Obtain path factory instance
            XPathFactory xpathFactory = XPathFactory.newInstance();
            // Create a new empty path
            XPath xpath = xpathFactory.newXPath();
            // Evaluate the path
            NodeList nodeList = (NodeList) xpath.evaluate(expression, doc,
                    XPathConstants.NODESET);
            return nodeList;
        } catch (XPathExpressionException e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * Saves XML Document into XML file.
     * 
     * @param fileName
     *            XML file name
     * @param doc
     *            XML document to save
     * @return true if method success false otherwise
     */
    public boolean saveXMLDocument(String fileName, Document doc) {
        boolean flag = false;
        try {
            // open output stream where XML Document will be saved
            FileOutputStream fos = new FileOutputStream(new File(fileName));
            // Use a Transformer for output
            TransformerFactory transformerFactory = TransformerFactory
                    .newInstance();
            Transformer transformer = transformerFactory.newTransformer();
            DOMSource source = new DOMSource(doc);
            StreamResult result = new StreamResult(fos);
            // transform source into result will do save
            transformer.transform(source, result);
            flag = true;
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (TransformerConfigurationException e) {
            e.printStackTrace();
        } catch (TransformerException e) {
            e.printStackTrace();
        }
        return flag;
    }
}
